package vip.liux.front.adapter.web.account;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.liux.contracts.security.UserDetailsInfo;
import vip.liux.front.infrastructure.utils.JwtConverter;

@RestController
@RequestMapping("/api/account/authentication")
public class AuthenticationController {

    private static final Logger log = LoggerFactory.getLogger(AuthenticationController.class);

    /**
     * 获取当前用户
     *
     * @param authentication 认证信息
     * @return 当前用户
     * @see org.springframework.security.core.userdetails.User
     * @see org.springframework.security.oauth2.jwt.Jwt
     */
    @GetMapping()
    public String register(Authentication authentication) {
        log.info("current user: {}", authentication.getName());
        return authentication.getName();
    }

    /**
     * 获取token
     *
     * @param authentication 认证信息
     * @return token
     */
    @PostMapping("/token")
    public JwtConverter.TokenModel authenticationToken(Authentication authentication) {
        UserDetailsInfo userDetail = (UserDetailsInfo) authentication.getPrincipal();
        return JwtConverter.generateToken(userDetail);
    }
    //endregion
}